package view.admin;

import controller.SeatController;
import model.Seat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 座位编辑对话框
 */
public class SeatEditDialog extends JDialog {
    private SeatController seatController;
    private Seat seat;
    private boolean confirmed = false;

    private JTextField seatNumberField;
    private JSpinner floorSpinner;
    private JTextField areaField;
    private JComboBox<String> statusComboBox;
    private JCheckBox hasComputerCheckBox;
    private JCheckBox hasPowerSocketCheckBox;
    private JTextArea equipmentTextArea;

    private JButton saveButton;
    private JButton cancelButton;

    public SeatEditDialog(JFrame parent, Seat seat) {
        super(parent, seat == null ? "添加座位" : "编辑座位", true);
        this.seat = seat;
        this.seatController = new SeatController();

        initComponents();
        setupLayout();
        setupEventListeners();

        if (seat != null) {
            loadSeatData();
        }
    }

    private void initComponents() {
        setSize(400, 500);
        setLocationRelativeTo(getParent());
        setResizable(false);

        seatNumberField = new JTextField(15);
        floorSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 10, 1));
        areaField = new JTextField(15);

        statusComboBox = new JComboBox<>();
        statusComboBox.addItem("可用");
        statusComboBox.addItem("已预约");
        statusComboBox.addItem("维护中");

        hasComputerCheckBox = new JCheckBox("配备电脑");
        hasPowerSocketCheckBox = new JCheckBox("有电源插座");

        equipmentTextArea = new JTextArea(3, 15);
        equipmentTextArea.setLineWrap(true);
        equipmentTextArea.setWrapStyleWord(true);

        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");
    }

    private void setupLayout() {
        setLayout(new BorderLayout());

        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(8, 8, 8, 8);

        // 座位号
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("座位号:"), gbc);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(seatNumberField, gbc);

        // 楼层
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("楼层:"), gbc);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(floorSpinner, gbc);

        // 区域
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("区域:"), gbc);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(areaField, gbc);

        // 状态
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.EAST;
        formPanel.add(new JLabel("状态:"), gbc);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(statusComboBox, gbc);

        // 设备选项
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.WEST;
        formPanel.add(hasComputerCheckBox, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        formPanel.add(hasPowerSocketCheckBox, gbc);

        // 设备信息
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.NORTHEAST;
        formPanel.add(new JLabel("设备信息:"), gbc);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        JScrollPane equipmentScrollPane = new JScrollPane(equipmentTextArea);
        formPanel.add(equipmentScrollPane, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void setupEventListeners() {
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleSave();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    private void loadSeatData() {
        seatNumberField.setText(seat.getSeatNumber());
        floorSpinner.setValue(seat.getFloor());
        areaField.setText(seat.getArea());
        statusComboBox.setSelectedIndex(seat.getStatus());
        hasComputerCheckBox.setSelected(seat.isHasComputer());
        hasPowerSocketCheckBox.setSelected(seat.isHasPowerSocket());
        equipmentTextArea.setText(seat.getEquipment());
    }

    private void handleSave() {
        // 验证输入
        String seatNumber = seatNumberField.getText().trim();
        String area = areaField.getText().trim();

        if (seatNumber.isEmpty() || area.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写完整的座位信息!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            if (seat == null) {
                // 添加新座位
                seat = new Seat();
            }

            seat.setSeatNumber(seatNumber);
            seat.setFloor((Integer) floorSpinner.getValue());
            seat.setArea(area);
            seat.setStatus(statusComboBox.getSelectedIndex());
            seat.setHasComputer(hasComputerCheckBox.isSelected());
            seat.setHasPowerSocket(hasPowerSocketCheckBox.isSelected());
            seat.setEquipment(equipmentTextArea.getText().trim());

            boolean success;
            if (seat.getSeatId() == 0) {
                success = seatController.addSeat(seat);
            } else {
                success = seatController.updateSeat(seat);
            }

            if (success) {
                confirmed = true;
                JOptionPane.showMessageDialog(this, "保存成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "保存失败!", "错误", JOptionPane.ERROR_MESSAGE);
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "保存失败: " + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public boolean isConfirmed() {
        return confirmed;
    }
}
